package com.example.compose_learn.compose

import android.view.LayoutInflater
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import com.example.compose_learn.databinding.HelloViewBinding
import com.example.compose_learn.ui.theme.ComposableTheme


@Preview(showBackground = true)
@Composable
fun EditText() {
    var value by remember { mutableStateOf("") }
    var name by remember { mutableStateOf("") }

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        TextField(
            value = value,
            onValueChange = { value = it },
            textStyle = TextStyle(fontSize = 30.sp),
            placeholder = {
                Text("请输入内容", fontSize = 25.sp)
            })

        Button(onClick = {
            name = value
        }) {
            Text("提交", fontSize = 30.sp)
        }

        Greeting(name)
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    if (name.isNotEmpty()){
        Text(
            text = "Hello $name!",
            modifier = modifier,
            fontSize = 30.sp
        )
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    ComposableTheme {
        Greeting("Android")
    }
}

@Composable
fun AndroidViewInCompose(){
    AndroidView(
        factory = { context ->
            val binding = HelloViewBinding.inflate(LayoutInflater.from(context))
            binding.button.setOnClickListener {
                binding.textView.text = "Hello from Compose!"
            }
            binding.root
        },
        update = { view ->
            val binding = HelloViewBinding.bind(view)
        },
        modifier = Modifier.fillMaxWidth()
    )
}